package com.ruoyi.experiment.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.experiment.domain.*;
import com.ruoyi.experiment.model.*;
import com.ruoyi.experiment.service.IExperimentOutService;
import com.ruoyi.experiment.service.IExperimentService;
import com.ruoyi.finance.model.Ids;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/experiment/out")
public class ExperimentOutController extends BaseController {

    @Autowired
    IExperimentOutService experimentService;

    // @PreAuthorize("@ss.hasPermi('experiment:experiment')")
    @GetMapping("/item/list")
    public TableDataInfo getAllItem(Project project){
        startPage();
        List<EProject> eProjects = experimentService.selectAllItem(project);
        return getDataTable(eProjects);
    }

    // @PreAuthorize("@ss.hasPermi('experiment:experiment')")
    @GetMapping("/sample/list")
    public TableDataInfo getSamplesByItem( Sample sample){
        startPage();
        List<ESample> samples = experimentService.listSamplesWithExperiment(sample);
        return getDataTable(samples);
    }

    /**
     * 导出
     * @param response
     * @param sample
     */
    @PostMapping("/sample/export")
    public void export(HttpServletResponse response, Sample sample)
    {
        List<ESample> list = experimentService.listSamplesWithExperiment(sample);
        ExcelUtil<ESample> util = new ExcelUtil<ESample>(ESample.class);
        util.exportExcel(response, list, "实验分发管理数据");
    }

    /**
     * 流转实验岗位
     * @param flume ids:样品id
     * @return
     */
    @PostMapping("/flume")
    public AjaxResult flume(@RequestBody ExperimentFlume flume){
        boolean f = experimentService.flume(flume);
        return AjaxResult.success();
    }

    /**
     * 查询实验管理列表
     */
    // @PreAuthorize("@ss.hasPermi('experiment:experiment:list')")
    @GetMapping("/list")
    public TableDataInfo list( Experiment experimentalManagement)
    {
        startPage();
        List<Experiment> list = experimentService.selectExperimentalManagementList(experimentalManagement);
        return getDataTable(list);
    }

    /**
     * 导出实验管理列表
     */
    // @PreAuthorize("@ss.hasPermi('experiment:experiment:export')")
    @Log(title = "实验管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Experiment experimentalManagement)
    {
        List<Experiment> list = experimentService.selectExperimentalManagementList(experimentalManagement);
        ExcelUtil<Experiment> util = new ExcelUtil<Experiment>(Experiment.class);
        util.exportExcel(response, list, "实验管理数据");
    }

    /**
     * 获取实验管理详细信息
     */
    // @PreAuthorize("@ss.hasPermi('experiment:experiment:query')")
    @GetMapping(value = "/{experimentId}")
    public AjaxResult getInfo(@PathVariable("experimentId") Long experimentId)
    {
        return success(experimentService.selectExperimentalManagementByExperimentId(experimentId));
    }

    /**
     * 分配岗位
     * @param experimentalManagement
     * @return
     */
    @Log(title = "实验管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Experiment experimentalManagement)
    {
        return toAjax(experimentService.updateExperimentalManagement(experimentalManagement));
    }

    /**
     * 取消分配岗位
     * @param id
     * @return
     */
    @Log(title = "实验管理", businessType = BusinessType.UPDATE)
    @GetMapping("cancel")
    public AjaxResult cancelPost(Long id)
    {
        return toAjax(experimentService.cancelExperiment(id));
    }

    /**
     * 批量分配岗位
     * @param experiments
     * @return
     */
    @Log(title = "实验管理", businessType = BusinessType.UPDATE)
    @PutMapping("posts")
    public AjaxResult setPosts(@RequestBody Experiments experiments)
    {
        for (Long id : experiments.getIds()) {
            Experiment experiment = experimentService.getExperimentBySampleId(id);
            experimentService
                    .updateExperimentalManagement(
                            experiments
                                    .getOne(
                                            experiment.getExperimentId(),
                                            experiment.getExpermentType(),
                                            experiment.getSampleId()));
        }
        return success();
    }
    /**
     * 选择岗位
     */
    @GetMapping("/job/list")
    public AjaxResult getJobList(Integer typeId){
        List<Job> allJob = experimentService.getAllJob(typeId);
        return success(allJob);
    }

    @GetMapping("getexp")
    public AjaxResult getExperimentBySampleId(Long id){
        Experiment experiment = experimentService.getExperimentBySampleId(id);
        return success(experiment);
    }

    /**
     * 完成外部实验
     * @param id
     * @return
     */
    @GetMapping("finish/out")
    public AjaxResult finishOutExperiment(Long id){
        boolean b = experimentService.finishOutExperiment(id);
        if (b)
            return success();
        else
            return new AjaxResult(208,"前置流程未完成",null);
    }

    /**
     * 显示指定项目下的所有样品
     * @param itemId
     * @return
     */
    @GetMapping("make/list")
    public AjaxResult getMakeList(Long itemId){
        List<TreeSample> makeList = experimentService.getMakeList(itemId);
        return success(makeList);
    }

    /**
     * 制作样品
     * @param sample
     * @return
     */
    @PostMapping("make")
    public AjaxResult makeSample(@RequestBody Sample sample){
        experimentService.makeSample(sample);
        return success();
    }

    /**
     * 退回样品
     * @param ids
     * @return
     */
    @GetMapping("back")
    public AjaxResult backSample(Long[] ids){
        boolean b = experimentService.backSample(Arrays.asList(ids));
        if (b)
            return success();
        else
            return new AjaxResult(208,"退回失败，请先完成前置流程",null);
    }

    @GetMapping("isFinish")
    public AjaxResult isFinish(Long id){
        boolean finish = experimentService.isFinish(id);
        return AjaxResult.success(finish);
    }

}
